//
//  QuoteList.m
//  Chart
//
//  Created by caojun on 14/12/8.
//
//

#import "ChartSocketLogger.h"

#import "YSSecurities.h"

#import "CJQuoteList.h"

#import <Foundation/Foundation.h>
#import "YSSecuritiesDB.h"


@interface CJQuoteList()

@property (nonatomic, strong) dispatch_queue_t concurrentQueue;


@end

@implementation CJQuoteList

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.concurrentQueue = dispatch_queue_create("com.ys.lg.YSQuoteList",
                                                     DISPATCH_QUEUE_CONCURRENT);
        
        [self fetchDataWithDB];
    }
    return self;
}

- (YSSecurities *)getQuoteWithID:(NSString *)ID {
    return [quoteMirror objectForKey:ID];
}

- (BOOL) addQuote:(YSSecurities *) quote {
    
    if (![self validQuote:quote]) return NO;

    if ([self getQuoteWithID:quote.IdStr]) {
        return NO;
    }

    dispatch_barrier_async(self.concurrentQueue, ^{
        [quoteMirror setObject:quote forKey:quote.IdStr];
    });
    return YES;
}

- (BOOL) mergeQuote:(YSSecurities *) quote {
    
    dispatch_barrier_async(self.concurrentQueue, ^{
        YSSecurities *storeQuote = [quoteMirror valueForKey:quote.IdStr];
        [storeQuote updateWithQuote:quote];
        if (storeQuote.IdStr) {
            [quoteMirror setValue:storeQuote forKey: storeQuote.IdStr];
        }
    });
    return YES;
}

- (BOOL)validQuote:(YSSecurities *) ss {
    
    if (!ss) return NO;
    if (!ss.isValidID) return NO;
    
    return YES;
}

- (NSDictionary *)getQuotes
{
    return quoteMirror;
}

- (void)reloadDataWithDB {
    
    [quoteMirror removeAllObjects];
    dispatch_barrier_async(self.concurrentQueue, ^{
        NSArray<YSSecurities *> *quoteArray = [[YSSecuritiesDB sharedManager] queryAllSecurities];
        
        for (YSSecurities *item in quoteArray) {
            [quoteMirror setObject:item forKey:item.IdStr];
        }
    });
}

- (void)fetchDataWithDB {
    
    quoteMirror = [NSMutableDictionary new];
    
    NSArray<YSSecurities *> *quoteArray = [[YSSecuritiesDB sharedManager] queryAllSecurities];
    
    for (YSSecurities *item in quoteArray) {
        [quoteMirror setObject:item forKey:item.IdStr];
    }
    
}

//- (NSArray *) filterQuoteListWidthServerId:(YTXServerId )serverId
//                               withIsLogin:(BOOL)isLogin
//                  withMarketQuoteSortArray:(NSArray *)marketQuoteSortArray
//{
//    NSMutableArray * list = [NSMutableArray new];
//    if (marketQuoteSortArray && marketQuoteSortArray.count) {
//        chartSocketLog(@"ChartController saveDataWith marketQuoteSortArray:%@", marketQuoteSortArray);
//        
//        for (NSDictionary *key in marketQuoteSortArray) {
//            YSSecurities * quote = [quoteMirror objectForKey:key[@"ID"]];
//            quote.AbbrName = key[@"AbbrName"];
//            quote.MarketTradingTarget = key[@"MarketTradingTarget"];
//            if (key[@"Unit"]) {
//                quote.Unit = key[@"Unit"];
//            }
//            
//            if (quote) {
//                [list addObject:quote];
//            }
//        }
//        
//        return list;
//    }
//    //==>wangsu  暂不排序
//    for (NSString *key in quoteMirror) {
//        YSSecurities * quote = [quoteMirror objectForKey:key];
////        NSNumber * clientTag = [quote getClientTagWidthServerId:serverId withIsLogin:isLogin];
////        NSInteger value = [clientTag integerValue];
////        
////        if (value > 0) {
//            [list addObject:quote];
////        }
//    }
//    return list;
////    return [list sortedArrayUsingComparator:^NSComparisonResult(YSSecurities * obj1, YSSecurities * obj2) {
////        NSInteger value1 = [[obj1 getClientTagWidthServerId:serverId withIsLogin:isLogin] integerValue];
////        
////        NSInteger value2 = [[obj2 getClientTagWidthServerId:serverId withIsLogin:isLogin] integerValue];
////        
////        if (value1 > value2) {
////            
////            return (NSComparisonResult)NSOrderedDescending;
////            
////        }
////        
////        if (value1 < value2) {
////            
////            return (NSComparisonResult)NSOrderedAscending;
////            
////        }
////        
////        return (NSComparisonResult)NSOrderedSame;
////    }];
//    
//}


@end
